
$(function () {
    var address_id = parseInt(GetQueryString('address_id'));
    var payorder_id = parseInt(GetQueryString('payorder_id'));
    var html_data = {
        baseConfig: baseConfig,
        sessionid: '',
        sessionid_q: '',
        address_info:{},
        payorder_id: payorder_id,
        province_list:[],
        city_list:[],
        county_list:[],
        address_list: []

    };
    var app = new Vue({
        el: '#address_modefy',
        data: html_data,
        methods: {
            query_county: function () {
                api.lorder.get_counties_list({city_id:html_data.address_info.receiver_city_id}).then(function (citydata) {
                    html_data.county_list = citydata.result.list;
                });
            },
            query_citys: function () {
                api.lorder.get_cities_list({province_id:html_data.address_info.receiver_province_id}).then(function (citydata) {
                    html_data.city_list = citydata.result.list;
                });
            },
            modefy_address: function () {
                var parm = {}

                parm.receiver_address_detail = html_data.address_info.receiver_address_detail;
                parm.receiver_city_id = html_data.address_info.receiver_city_id;
                parm.receiver_county_id = html_data.address_info.receiver_county_id;
                parm.receiver_name = html_data.address_info.receiver_name;
                parm.receiver_phone = html_data.address_info.receiver_phone;
                parm.receiver_province_id = html_data.address_info.receiver_province_id;
                parm.is_default = html_data.address_info.is_default;
                if(html_data.address_info.id){
                    parm.address_id = html_data.address_info.id;
                    if(payorder_id){
                        parm.payorder_id = payorder_id;
                    }
                    api.lorder.update_address(parm) .then(function (data) {
                        if(payorder_id) {
                            baseConfig.page_back();
                            // location.href = 'user_address.html?payorder_id=' + payorder_id + '&address_id=' + address_id;
                        }else{
                            Magiclamp.mcAlert("提示", "修改邮寄地址成功", "知道了", function () {
                                // location.href = 'user_address.html';
                                baseConfig.page_back();
                            });

                        }
                    });
                }else{
                    api.lorder.create_address(parm).then(function (data) {
                        // history.go(-1);
                        if(payorder_id) {
                            baseConfig.page_back();
                            // location.href = 'user_address.html?payorder_id=' + payorder_id + '&address_id=' + address_id;
                        }else{
                            Magiclamp.mcAlert("提示", "修改邮寄地址成功", "知道了", function () {
                                // location.href = 'user_address.html';
                                baseConfig.page_back(-2);
                            });
                        }
                    })
                }

            },
            page_back: function () {
                baseConfig.page_back();
            }
        }
    });

    html_data.sessionid = GetQueryString('sessionid');
    if (html_data.sessionid) {
        html_data.sessionid_q = "sessionid=" + html_data.sessionid;
        client_storage('sessionid', html_data.sessionid);
    }
    
    api.lorder.get_provinces_list().then(function (data) {
        html_data.province_list = data.result.list;
    });

    if(address_id){
        api.lorder.get_address({address_id:address_id}).then(function (data) {
            html_data.address_info = data.result;

            api.lorder.get_cities_list({province_id:html_data.address_info.receiver_province_id}).then(function (citydata) {
                html_data.city_list = citydata.result.list;
            });
            api.lorder.get_counties_list({city_id:html_data.address_info.receiver_city_id}).then(function (citydata) {
                html_data.county_list = citydata.result.list;
            });
        });
    }else{
        html_data.address_info = {receiver_province_id:0, receiver_city_id:0, receiver_county_id:0};
    }


});
